﻿using Quartz;
using System;

namespace Zocono.WCS.HangfireHost.QuartzCore
{
    public class QuartzHelp : IQuartzHelp
    {
        public IJobDetail CreateJob<T>(string jobCode, string groupCode, bool isOrderly = false) where T : IJob
        {
            return JobBuilder.Create<T>().WithIdentity(jobCode, groupCode).DisallowConcurrentExecution(isOrderly)
                .Build();
        }

        public IJobDetail CreateJob(Type type, string jobCode, string groupCode, bool isOrderly = false)
        {
            return JobBuilder.Create(type).WithIdentity(jobCode, groupCode).DisallowConcurrentExecution(isOrderly)
                .Build();
        }

        public IJobDetail CreateJob(Type type, string jobCode, string groupCode, JobDataMap pairs, bool isOrderly = false)
        {
            return JobBuilder.Create(type).WithIdentity(jobCode, groupCode).DisallowConcurrentExecution(isOrderly).SetJobData(pairs)
                .Build();
        }

        public ITrigger CreateTrigger(string triggerCode, string groupCode, string cronExp, IJobDetail job, Action<CronScheduleBuilder> action)
        {
            return TriggerBuilder.Create().WithIdentity(triggerCode, groupCode).WithCronSchedule(cronExp, action)
                .ForJob(job)
                .Build();
        }

        /// <summary>
        /// 执行一次
        /// </summary>
        /// <param name="triggerCode"></param>
        /// <param name="groupCode"></param>
        /// <param name="job"></param>
        /// <returns></returns>
        public ISimpleTrigger CreateTrigger(string triggerCode, string groupCode, IJobDetail job)
        {
            ITrigger trigger = TriggerBuilder.Create().WithIdentity(triggerCode, groupCode).StartNow()
                .ForJob(job)
                .Build();
            return (ISimpleTrigger)trigger;
        }
    }
}
